<?php
//NICEMIP.COM [Don't forget the beginner's mind]
//Copyright (c) 2017~2099 http://NICEMIP.COM All rights reserved.
namespace app\setting\controller;
use think\Request;
use mip\AdminBase;
class ApiAdminSetting extends AdminBase
{
    public function settingSelect(){
        $settingInfo = json_decode(input('post.setting'));
        $settings = db($this->settings)->where('id_org', config('orgId'))->select();
        
        foreach ($settings as $k => $v){
            $this->mipInfo[$v['key']] = $v['val'];
        }
        $siteImgs = explode(',',$this->mipInfo['siteImg']);
        foreach($siteImgs as $key => $val){
            $siteImgs[$key] = $this->mipInfo['articleDomain'].'/uploads/'.$val;
        }
        $siteCount = count($siteImgs);
        $this->mipInfo['siteData'] = array(
            'siteImgArr' => $siteImgs,
            'siteNum' => $siteCount,
        );
        $this->mipInfo['siteLogos'] = $this->mipInfo['articleDomain'].'/uploads/'.$this->mipInfo['siteLogo'];
        $this->mipInfo['siteLogo'] = $this->mipInfo['siteLogo'];
        return jsonSuccess('',$this->mipInfo);
    }
    

    public function settingEdit(){

        $settingInfo = json_decode(input('post.setting'));

        foreach ($settingInfo as $key => $val) {
            db($this->settings)->where('id_org', config('orgId'))->where( "`key` = '" . $key . "'")->update(array('val' => $val));
        }
        return jsonSuccess('保存成功');
    }
    
    public function settingSave() {
        $domain = input('post.domain');
        $siteName = input('post.siteName');
        $httpType = input('post.httpType');
        
        db($this->settings)->where('id_org', config('orgId'))->where('key','domain')->update(array('val' => $domain));
        db($this->settings)->where('id_org', config('orgId'))->where('key','siteName')->update(array('val' => $siteName));
        db($this->settings)->where('id_org', config('orgId'))->where('key','httpType')->update(array('val' => $httpType));
        
        if (input('post.setting')) {
            $settingInfo = json_decode(input('post.setting'));
            foreach ($settingInfo as $key => $val) {
                db($this->settings)->where('id_org', config('orgId'))->where( "`key` = '" . $key . "'")->update(array('val' => $val));
            }
        }
        return jsonSuccess('保存成功');
    }
    /**
    *图片上传
    */
    public function settingUpload(){
    	 $files = Request::instance()->file('files');
    	 $type = 'setting';
    	 $return = [];
    	 foreach($files as $file){
    	 	$fileName = $type . '/' . date('Y',time()) . '/' . date('m',time()) . '/' . date('d',time());
         	$info = $file->rule('uniqid')->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . '/' . $this->mipInfo['uploadUrl'] . '/' . $type . '/' . date('Y',time()) . '/' . date('m',time()) . '/' . date('d',time()));
         	$fileName = $fileName.'/'.$info->getFilename();
         	$file_name .= ','.$fileName;
         	$siteImgArr[] = $this->mipInfo['articleDomain'].'/uploads/'.$fileName;
    	 }
    	 $file_name = trim($file_name,',');
    	 $return = [
    	 	'file_name' => $file_name,
    	 	'siteImgArr' => $siteImgArr
    	 ];
          return jsonSuccess('保存成功',$return);
    }


}